package com.springboot.huli.controller;


import com.springboot.huli.entity.Hushi;
import com.springboot.huli.entity.SysUser;
import com.springboot.huli.entity.Zhuren;
import com.springboot.huli.entity.vo.HushiVo;
import com.springboot.huli.entity.vo.ZhurenVo;
import com.springboot.huli.qpage.QPage;
import com.springboot.huli.service.IHushiService;
import com.springboot.huli.service.ISysUserService;
import com.springboot.huli.service.IZhurenService;
import com.springboot.huli.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 主任表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-28
 */
@Controller
@RequestMapping("/api/admin/zhuren")
public class ZhurenController {


    /**
     * 路由地址
     */
    public String prefix = "/view/admin/zhuren";

    /**
     * 业务层
     */
    @Autowired
    private IZhurenService service;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


    /**
     * 获取所有的列表
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result getList(QPage qPage, ZhurenVo obj) {
        return Result.reOk("查询成功",this.service.getPageList(qPage,obj));
    }


    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return prefix+"/add";
    }

    /**
     * 新增保存
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(ZhurenVo obj) {
        return this.service.insert(obj);
    }

    /**
     * 进入编辑页面
     * @return
     */
    @RequestMapping("/edit")
    public String edit(){
        return prefix+"/edit";
    }

    /**
     * 根据ID获取信息
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public Result getById(Integer id) {
        ZhurenVo zhurenVo=new ZhurenVo();
        Zhuren zhuren=this.service.getById(id);
        BeanUtils.copyProperties(zhuren,zhurenVo);
        SysUser sysUser=this.iSysUserService.getById(id);
        zhurenVo.setPwd(sysUser.getPwd());
        zhurenVo.setAccount(sysUser.getAccount());
        return Result.reOk("查询成功",zhurenVo);
    }

    /**
     * 编辑保存
     * @return
     */
    @RequestMapping("/updates")
    @ResponseBody
    public Result updates(ZhurenVo obj) {
        return this.service.updates(obj);
    }


    /**
     * 删除保存
     * @return
     */
    @RequestMapping("/deleteId")
    @ResponseBody
    public Result deleteId(int id) {
        iSysUserService.removeById(id);
        boolean isOk=this.service.removeById(id);
        if(isOk){
            return Result.reOk("删除成功");
        }
        return Result.reError("删除失败");
    }


}
